package ftpproxy;

import java.io.FileWriter;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

public class Log {
	public static String CRLF = "\r\n";

	public static void write(Configuration config, String userId, String cmdMode, 
		String message) {

		String dirPath = config.logDir + "/"; 
		String fileName = DateFormatUtils.format(new Date(), "yyyyMMdd") + ".log";

		FileWriter fileWriter = null;

		String userid = "NOLGN";
		if (!StringUtils.isEmpty(userId)) 
			userid = userId;

		try {
			fileWriter = new FileWriter(dirPath + fileName, true);
			fileWriter.write(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss") + " " + userid
				+ " <" + cmdMode + "> -> " + message + CRLF);
		} catch (Exception ex) {
			System.out.println("*** Log File Exception caught ***");
			System.out.println("Message: " + ex.getMessage());
		} finally {
			try {
				if (fileWriter != null)
					fileWriter.close();
			} catch (Exception e) {}
		}
	}

	public static void write(Configuration config, String message) {
		String dirPath = config.logDir + "/"; 
		String fileName = DateFormatUtils.format(new Date(), "yyyyMMdd") + ".log";

		FileWriter fileWriter = null;

		try {
			fileWriter = new FileWriter(dirPath + fileName, true);
			fileWriter.write(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss") + " NOCON <LOG> -> " + message + CRLF);
		} catch (Exception ex) {
			System.out.println("*** Log File Exception caught ***");

			System.out.println("Message: " + ex.getMessage());
		} finally {
			try {
				if (fileWriter != null)
					fileWriter.close();
			} catch (Exception e) {}
		}
	}
}
